package com.jb.repair.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jb.repair.R;
import com.jb.repair.model.LedgerModelTest;
import com.jb.repair.view.IconFontTextView;

import java.util.ArrayList;

/**
 * Created by SLF on 2015/12/23.
 */
public class TreeLedgerAdapter extends BaseAdapter {

    private ArrayList<LedgerModelTest> elementsData;
    private ArrayList<LedgerModelTest> elements;
    private LayoutInflater inflater;
    private int indentionBase;
    private Context context;
    private JumpFragment jumpFragment;

    public TreeLedgerAdapter(ArrayList<LedgerModelTest> elements,
                             ArrayList<LedgerModelTest> elementsData, Context context, JumpFragment jumpFragment) {
        this.elements = elements;
        this.elementsData = elementsData;
        inflater = LayoutInflater.from(context);
        this.context = context;
        indentionBase = 30;
        this.jumpFragment = jumpFragment;
    }

    public ArrayList<LedgerModelTest> getElements() {
        return elements;
    }

    public ArrayList<LedgerModelTest> getElementsData() {
        return elementsData;
    }

    @Override
    public int getCount() {
        return elements.size();
    }

    @Override
    public Object getItem(int position) {
        return elements.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder holder = null;
//        if (convertView == null) {
//            holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.item_tree, null);
        IconFontTextView disclosureImg = (IconFontTextView) convertView
                .findViewById(R.id.icon);
        TextView mainText = (TextView) convertView
                .findViewById(R.id.tv_main);
        TextView tv_right = (TextView) convertView
                .findViewById(R.id.tv_right);
//            convertView.setTag(holder);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }
        final LedgerModelTest element = elements.get(position);
        int level = element.getLevel();
        boolean b = element.isHasChildren();
        if (level == 0) {
            convertView.setBackgroundResource(R.drawable.first);
        } else if (level == 1) {
            convertView.setBackgroundResource(R.drawable.second);
        } else if (level == 2) {
            convertView.setBackgroundResource(R.drawable.third);
        } else if (level == 3) {
            convertView.setBackgroundResource(R.drawable.fourth);
        } else if (level == 4) {
            convertView.setBackgroundResource(R.drawable.fifth);
        } else {
            convertView.setBackgroundResource(R.drawable.others);
        }
        if(!b){
            convertView.setBackgroundResource(R.drawable.last);
            tv_right.setVisibility(View.INVISIBLE);
           disclosureImg.setVisibility(View.INVISIBLE);
        }


        mainText.setPadding(indentionBase * (level),
                mainText.getPaddingTop(),
                mainText.getPaddingRight(),
                mainText.getPaddingBottom());
        mainText.setText(element.getContentText().layer_name);
//        if (element.isHasChildren() && !element.isExpanded()) {
//            holder.disclosureImg.setVisibility(View.VISIBLE);
//        } else if (element.isHasChildren() && element.isExpanded()) {
//            holder.disclosureImg.setVisibility(View.VISIBLE);
//        } else if (!element.isHasChildren()) {
//            holder.disclosureImg.setVisibility(View.INVISIBLE);
//        }

        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击跳转到台账页面
//                Intent intent = new Intent(context, LedgerFrament.class);
//                intent.putExtra("levelinfo",elements.get(position));
//                context.startActivity(intent);
//                Toast.makeText(context,"跳转到设备台账页面"+element.getContentText().guid,Toast.LENGTH_SHORT).show();
                jumpFragment.toFragment(element);
            }
        });
        return convertView;
    }
    public static class JumpFragment{
        public void toFragment(LedgerModelTest ledgerModelTest){

        }
    }

//    static class ViewHolder {
//        TextView mainText;
//        IconFontTextView disclosureImg;
//        TextView tv_right;
//    }
}
